/*
    代码有些地方不是很合理，不过由于时间比较赶，结果是正确的也就没有再做调整
    write by lei.jiangl
*/
var https = require('https');
//var sqlite3 = require('sqlite3').verbose();
var querystring = require('querystring');
var ET = require('elementtree');
//var db = new sqlite3.Database('/Users/jiangleivos/alibaba-work/DB/db');
//var $ = require('jquery');
var fs = require('fs');
var idx = 1;
//var workSpace = '/Users/jiangleivos/alibaba-work'
var workSpace = '/home/pts/alibaba-work/'
var timeNow = new Date();
// var tableName = 'test_Id';
// ET.register_namespace('soapenv', 'http://schemas.xmlsoap.org/soap/envelope/');
// ET.register_namespace('v201', 'https://adwords.google.com/api/adwords/cm/v201302');
// ET.register_namespace('v20', 'https://adwords.google.com/api/adwords/o/v201302');


var SOAP = {
    'soapenv': 'http://schemas.xmlsoap.org/soap/envelope/',
    'ns_xsd': 'http://www.w3.org/1999/XMLSchema',
    'ns_xsi': 'http://www.w3.org/2001/XMLSchema-instance',
    'ns_soapenc': 'http://schemas.xmlsoap.org/soap/encoding/',
    'v201': 'https://adwords.google.com/api/adwords/cm/v201302',
    'v20': 'https://adwords.google.com/api/adwords/o/v201302'
};

// var path = '/home/pts/alibaba-work/report/report/keywordIdea/';
// var path = workSpace+'/report/report/keywordIdea/';
var path = workSpace+'/report/report/keywordIdea/';
var rstPath = workSpace+'/report/report/targetIdeaResult/';
var arg1 = process.argv[2] || 0;
var arg2 = process.argv[3] || 0;

function getToken(callback) {
    // console.log('-----------------------------------------------------getToken');
    logTime('getToken start');
    var qstr = querystring.stringify({
        Email: 'ptapitest@gmail.com',
        Passwd: 'aA123456',
        accountType: 'GOOGLE',
        source: 'adwords-tutorial',
        service: 'adwords'
    });
    // console.log(qstr);
    var sconf = {
        host: 'www.google.com',
        // host:'74.125.128.147',
        path: '/accounts/ClientLogin' + '?' + qstr,
        method: 'GET',
        headers: {}
    };
    var req = https.request(sconf, function(res) {
        // console.log(res);
        res.on('data', function(data) {
            var t = data.toString().split('\n')[2];
            var token = t.split('Auth=')[1];
            logTime('getToken end');
            callback(token);
        });

    })
    req.end();
}


function sendSOAP(xml, callback) {
    // console.log('-----------------------------------------------------sendSOAP');
    logTime('sendSOAP start');

    var soapStr = toXMLString.call(xml);
    var conf = {
        host: 'adwords.google.com',
        // host:'74.125.128.51',
        method: 'POST',
        header: {
            // 'Accept-Encoding': 'gzip,deflate',
            'Content-Type': 'text/xml;charset=UTF-8',
            'Content-Length': soapStr.length
        },
        path: '/api/adwords/o/v201302/TargetingIdeaService?wsdl'
    }
    var req = https.request(conf, function(res) {
        var dataStr = '';
        logTime('res.statusCode=' + res.statusCode);
        // if(res.statusCode === 500) {
        //     console.log(res);
        // }
        // console.log('===============================', res.statusCode);
        res.on('end', function() {
            // console.log(dataStr);
            logTime('sendSOAP end');
            if (res.statusCode === 500) {
                if(dataStr.indexOf('RateExceededError')!=-1){
                    sendSOAP(xml,callback);
                }
                console.log(soapStr);
                console.log(dataStr);
                
            } else {
                callback(dataStr);
            }

        })
        var count = 0;
        res.on('data', function(data) {
            dataStr += data.toString();
        });

    });
    // console.log('+++++++++++++++++++++++++++++++\n',soapStr)
    req.write(soapStr);
    req.end();
}

function getKeywordMap(path) {
    var fileArr = [],
        keywordMap = {};
    var files = fs.readdirSync(path);
    files.forEach(function(name) {
        if (name.split('.')[0] !== '') {
            fileArr.push(name);
        }
    })

    fileArr.forEach(function(fileName) { //遍历读取文件夹下所有文件
        var data = fs.readFileSync(path + fileName, 'utf-8');
        // console.log(data);
        var fa = data.toString().split('\r\n');
	//console.log(fa)
	if(fa[fa.length-1]===''){
            fa.pop();
	}
        keywordMap[fileName] = [];
        // console.log(keywordMap);
        fa.forEach(function(keyword) {
            if (keyword !== '') {
                keywordMap[fileName].push(keyword);
                // console.log(keyword);
            }
        });
    })
    return keywordMap;
}

function getGoogleData(xml, keywordMap) {
    logTime('getGoogleData start');
    var kIndex = 0,
        keywordIdx = 0;
    // console.log(xml);
    var keywordEstimate = xml.find('.//v20:keywordEstimateRequests');
    var keywordEstimate = ET.Element('v20:keywordEstimateRequests');
    var searchParameters = xml.find('.//v20:searchParameters');

    var keyArr = [];
    for (var k in keywordMap) {
        keyArr.push(k);
    }

    function _getGoolgeData(kIndex, keywordIdx, callback) {
        var key = keyArr[kIndex];
        var keyword = keywordMap[key][keywordIdx];
	//console.log(keywordMap);
	//console.log(keyword,key,keywordIdx);
        logTime(kIndex+'  '+keywordIdx+'  '+key + '  '+ 'keyword=['+keyword+'] begin');
        searchParameters.clear();
        searchParameters.set('ns_xsi:type', 'v20:RelatedToQuerySearchParameter');
        var query = ET.Element('{' + SOAP['v20'] + '}queries');
        query.text = keyword;
        searchParameters.append(query);
        sendSOAP(xml, function(xmlStr) {//发送soap请求
            var x = ET.XML(xmlStr);
            var datas = x.findall('.//data');
            var keywordIdeaArr = [];
            datas.forEach(function(data){
                var keywordIdea = data.find('./value/value').text;
                keywordIdeaArr.push(keywordIdea);
            });
            writeFile(key,keyword,keywordIdeaArr);//按照业务逻辑写文件
            logTime('kIndex='+kIndex+'  '+'keywordIdx='+keywordIdx+'  '+'fileName='+key + '  '+ 'keyword=['+keyword+'] end');
            if(keywordIdx<keywordMap[key].length-1){
                keywordIdx ++;
                setTimeout(function (argument) {
                    // body...
                    callback(kIndex, keywordIdx,_getGoolgeData);
                },0)
                
            }else {
                keywordIdx = 0;
                if(kIndex<keyArr.length-1){
                    kIndex ++;
                    setTimeout(function (argument) {
                        // body...
                        callback(kIndex, keywordIdx,_getGoolgeData);
                    },0)
                }
            }
            
            // console.log(x);
        });
    }
    _getGoolgeData(parseInt(arg1), parseInt(arg2), _getGoolgeData);
    // logTime('getGoogleData end');
}
function checkKeyword(keyword,comKeyword){
    // return true;
    // console.log(keyword,comKeyword);
    var reg = /(www\.|\.org|\.net|\.gov|\.edu|\.ie|\.sg|what|\.com|buy|\.co\.uk|how to)/;
    if(reg.test(comKeyword)){//包含what .com buy .co .uk how to的不要
        return false;
    }
    // console.log(keyword);
    try{
        var reg2 = new RegExp('the(\\s)+'+keyword);    
    }catch(e){
        var err = e.toString();
        var errKeyword = 'error keyword = '+keyword;
        log(err);
        log(errKeyword);
        return false;
    }


    //包含 the+空格+关键词的不要
    if(reg2.test(comKeyword)){
        return false;
    }
    if (comKeyword.indexOf(keyword) !== -1 && comKeyword !== keyword + 's') { //判断逻辑，确认包含并且不是Keyword + 's'
        return true;
    }
    return false;
}
function writeFile(fileName,keyword,keywordIdeas){
    var _path = rstPath + fileName.split('.')[0]+'_targetIdeas.txt';
    var arr = [];
    keywordIdeas.forEach(function(keywordIdea) {
        // console.log(keywordIdea,',',keyword);
        if(checkKeyword(keyword,keywordIdea)){
            arr.push(keywordIdea);
        }
        // if (keywordIdea.indexOf(keyword) !== -1 && keywordIdea !== keyword + 's') { //判断逻辑，确认包含并且不是Keyword + 's'
        //     arr.push(keywordIdea);
        // }
    });

    //arr.push('\r\n');
    var str = keyword + '\t' + arr.join(',') + '\r\n';
    // console.log(str);
    // console.log(str);
    // logTime(keyword);
    fs.appendFileSync(_path,str);
}
function log(str){
    var path = rstPath + 'log';
    fs.appendFileSync(path,str+'\n');
}
function main() {
    var keywordMap = getKeywordMap(path);
    logTime('main ' + timeNow);
    var str = fs.readFileSync('../soapXML/googleTargetingIdea.xml').toString();
    var xml = ET.XML(str);
    getGoogleData(xml, keywordMap);
    return;
    getToken(function(AuthToken) {
        console.log('-----------------------------------------------------getToken')
        xml.find('.//v201:authToken').text = AuthToken;
        getGoogleData(xml, keywordMap);

    });
}

function toXMLString() {
    return ET.tostring(this, {
        xml_declaration: false
    });
}

function logTime(str) {
    console.log('----------------------------------------------------- ' + str + '   ', (new Date().getTime() - timeNow.getTime()) / 1000 + 'S');
}
main();

